// ListeningSocket.cpp : implementation file
//

#include "stdafx.h"
#include "ListeningSocket.h"
#include "CommWithClient.h"
#include "ServerSocket.h"
#include "ServerSocketThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CListeningSocket

CListeningSocket::CListeningSocket()
{
}

CListeningSocket::~CListeningSocket()
{
}

// Do not edit the following lines, which are needed by ClassWizard.
#if 0
BEGIN_MESSAGE_MAP(CListeningSocket, CAsyncSocket)
	//{{AFX_MSG_MAP(CListeningSocket)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()
#endif	// 0

/////////////////////////////////////////////////////////////////////////////
// CListeningSocket member functions

void CListeningSocket::OnAccept(int nErrorCode)
{
	// TODO: Add your specialized code here and/or call the base class
	CServerSocket ServerSocket;
	SOCKADDR_IN SockAddr;
	int nSockAddrLen = sizeof(SOCKADDR_IN);
	Accept(ServerSocket, (SOCKADDR *)&SockAddr, &nSockAddrLen);

	CServerSocketThread * pServerSocketThread = (CServerSocketThread *)AfxBeginThread(RUNTIME_CLASS(CServerSocketThread), THREAD_PRIORITY_NORMAL, 0, CREATE_SUSPENDED);
	pServerSocketThread->m_hSocket = ServerSocket.Detach();

	CommWithClient::Instance()->SendNotification(WM_APP_CONNECTED, (HANDLE)pServerSocketThread->m_hSocket, SockAddr.sin_addr.S_un.S_addr);

	VERIFY(pServerSocketThread->ResumeThread());

	CAsyncSocket::OnAccept(nErrorCode);
}